-- 8. The Data.List module defines a function, groupBy, which has the following type:
-- -- file: ch04/ch04.exercises.hs
-- groupBy :: (a -> a -> Bool) -> [a] -> [[a]]

-- 9. Use ghci to load the Data.List module and figure out what groupBy does, then
-- write your own implementation using a fold.

groupBy_foldr :: (a -> a -> Bool) -> [a] -> [[a]]
groupBy_foldr p xs = foldr step [] xs
  where step x (y:acc) = if p x (head y)
                         then (x:y):acc
                         else [x]:(y:acc)
        step x [] = [[x]]

-- 测试用例
-- groupBy_foldr (==) "aaabcccdda"
-- ["aaa","b","ccc","dd","a"]

-- groupBy_foldr (<=) [1,2,2,3,1,2,0,4,5,2]
-- [[1,2,2,3],[1,2],[0,4,5],[2]]
